#!/bin/bash
tar -zxvf cmake-2.8.11.2.tar.gz &&
cd cmake-2.8.11.2 &&
./configure &&
make && make install


groupadd mysql && useradd -g mysql mysql -s /bin/false &&
mkdir -p /data/mysql &&
chown -R mysql:mysql /data/mysql


mkdir -p /usr/local/mysql &&
cd /usr/local/src &&
tar -zxvf mariadb-5.5.39.tar.gz &&
cd mariadb-5.5.39 &&
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc &&
make && make install

mv /etc/my.cnf /etc/my.cnf.bak

cd /usr/local/mysql &&
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql &&
ln -s /usr/local/mysql/my.cnf /etc/my.cnf &&
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld &&
chmod  /etc/init.d/mysqld &&
chkconfig mysqld on

sed -i  "46cbasedir=/usr/local/mysql" /etc/rc.d/init.d/mysqld && sed  -i  "47cdatadir=/data/mysql" /etc/rc.d/init.d/mysqld
echo 'export PATH=$PATH:/usr/local/mysql/bin'>>/etc/profile && source /etc/profile

ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql &&
ln -s /usr/local/mysql/include/mysql /usr/include/mysql &&
mkdir /var/lib/mysql &&
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock